package com.zmn.oms.services.impl.orderworkingrecord;

import com.zmn.base.plat.engine.common.constant.PlatEngineConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.model.entity.orderworkingrecord.OrderWorkingRecord;
import com.zmn.oms.model.entity.orderworkingrecord.OrderWorkingRecordQuery;
import com.zmn.oms.persistence.interfaces.orderworkingrecord.OrderWorkingRecordDao;
import com.zmn.oms.services.interfaces.orderworkingrecord.OrderWorkingRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 订单-服务录音接口实现类
 *
 * @author chengguang
 * @since  2022/07/12 10:02
 */
@Slf4j
@Service
public class OrderWorkingRecordServiceImpl implements OrderWorkingRecordService {

    private static final String TAG = "订单-服务录音基础服务接口";

    @Resource
    private OrderWorkingRecordDao orderWorkingRecordDao;

    @Override
    public OrderWorkingRecord findById(Integer recordId) {
        if (Objects.isNull(recordId)) {
            log.error("[{}],根据id查询数据recordId为null", TAG);
            return null;
        }
        return orderWorkingRecordDao.findById(recordId);
    }

    @Override
    public Integer findMaxSort() {
        return orderWorkingRecordDao.findMaxSort();
    }

    @Override
    public List<OrderWorkingRecord> listByIds(List<Integer> recordIds) {
        if (CollectionUtils.isEmpty(recordIds)) {
            log.error("[{}],按条件查询数据,recordIds为null", TAG);
            return Collections.emptyList();
        }
        return orderWorkingRecordDao.listByIds(recordIds);
    }

    @Override
    public List<OrderWorkingRecord> listByQuery(OrderWorkingRecordQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        return orderWorkingRecordDao.listByQuery(query);
    }

    @Override
    public List<OrderWorkingRecord> listPageByQuery(OrderWorkingRecordQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return Collections.emptyList();
        }
        Integer count = countByQuery(query);
        query.setTotalCount(count);
        if (Objects.equals(count,PlatEngineConsts.NONE)){
            return new ArrayList<>();
        }
        return orderWorkingRecordDao.listPageByQuery(query);
    }

    @Override
    public Integer countByQuery(OrderWorkingRecordQuery query) {
        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return orderWorkingRecordDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(OrderWorkingRecord orderWorkingRecord) {
        if (Objects.isNull(orderWorkingRecord.getRecordId())){
            log.error("[{}],根据id修改数据,orderWorkingRecord为null", TAG);
            return null;
        }
        return orderWorkingRecordDao.updateByKey(orderWorkingRecord);
    }

    @Override
    public void updateStatusBatch(List<Integer> ids, Integer status, String operator) {
        if (!Objects.equals(status, PlatEngineConsts.NO) && !Objects.equals(status, PlatEngineConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        orderWorkingRecordDao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert(OrderWorkingRecord orderWorkingRecord){
        if (Objects.isNull(orderWorkingRecord)){
            log.error("[{}],新增数据,orderWorkingRecord为null", TAG);
            return null;
        }
        return orderWorkingRecordDao.insert(orderWorkingRecord);
    }

    @Override
    public void insertBatch(List<OrderWorkingRecord> list){
        if (CollectionUtils.isEmpty(list)) {
            log.error("[{}],数据为空", TAG);
            return;
        }
        orderWorkingRecordDao.insertBatch(list);
    }

    @Override
    public Integer deleteByKey(Integer recordId) {
        if (Objects.isNull(recordId)){
            log.error("[{}],根据id删除数据,recordId为null", TAG);
            return null;
        }
        return orderWorkingRecordDao.deleteByKey(recordId);
    }
}